﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SMARTHOTEL.MenusDto;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace SMARTHOTEL.Menus
{
    /// <summary>
    /// 菜单接口
    /// </summary>
    [Route("api/v2/Menus")]
    [ApiExplorerSettings(GroupName = "v2")]
    public class MenusService : ApplicationService, IMenusService
    {
        // 菜单仓储，用于对菜单数据进行增删查改操作
        private readonly IRepository<Menu> Menurepository;

        // 菜单类型仓储，用于对菜单类型数据进行增删查改操作
        private readonly IRepository<MenuType> MenuTyperepository;

        // 规格仓储，用于对规格数据进行增删查改操作
        private readonly IRepository<Specs> Specsrepository;

        /// <summary>
        /// 构造函数，初始化菜单服务类
        /// </summary>
        /// <param name="Menurepository">菜单仓储接口</param>
        /// <param name="MenuTyperepository">菜单类型仓储接口</param>
        /// <param name="Specsrepository">规格仓储接口</param>
        public MenusService(IRepository<Menu> Menurepository, IRepository<MenuType> MenuTyperepository, IRepository<Specs> Specsrepository)
        {
            this.Menurepository = Menurepository;
            this.MenuTyperepository = MenuTyperepository;
            this.Specsrepository = Specsrepository;
        }



        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPost("addmenu")]
        public async Task<APIResult> AddMenu(MenuDto menu)
        {
            var list = await Menurepository.FirstOrDefaultAsync(t => t.MenuName == menu.MenuName);
            if (list != null)
            {
                return APIResult.Fail("菜单已存在");
            }
            var entity = ObjectMapper.Map<MenuDto, Menu>(menu);
           // menu.Status = true;
            await Menurepository.InsertAsync(entity);
            return APIResult.OK(data: entity);
        }
        /// <summary>
        /// 添加菜单类型
        /// </summary>
        /// <param name="menuType"></param>
        /// <returns></returns>
        [HttpPost("addmenuType")]
        public async Task<APIResult> AddMenuType(MenuType menuType)
        {
            var list = await MenuTyperepository.InsertAsync(menuType);
            return APIResult.OK(data: list);
        }
        /// <summary>
        /// 获取菜单类型
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetmenuType")]
        public async Task<APIResult> GetMenusType()
        {
            var list = await MenuTyperepository.GetListAsync();
            return APIResult.OK(data: list);
        }
        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet("Getmenu")]
        public async Task<APIResult> GetMenu()
        {
            var list = (from a in await MenuTyperepository.GetQueryableAsync()
                        join
                        b in await Menurepository.GetQueryableAsync()
                        on a.Id equals b.MenuTYpeId
                        select new GetMenuDto
                        {
                            MenuId = b.Id,
                            MenuName = b.MenuName,
                            MenuImg = b.MenuImg,
                            MenuDesc = b.MenuDesc,
                            Price = b.Price,
                            inventory = b.inventory,
                            Taste = b.Taste,
                            Sellingperiod = b.Sellingperiod,
                            Status = b.Status,
                            MenuTYpeId = a.Id,
                            MenuTypeName = a.MenuTypeName
                        }).ToList();
            return APIResult.OK(data: list);
        }
        /// <summary>
        /// 反填菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("FanMenu")]
        public async Task<APIResult> FanMenu(Guid id)
        {
            var list = await Menurepository.FirstOrDefaultAsync(t => t.Id == id);
            return APIResult.OK(data: list);
        }
        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="menu">菜单</param>
        /// <returns></returns>
        [HttpPut("UpdMenu")]
        public async Task<APIResult> UpdMenu(UpdateMenuDto menu)
        {
            var list = await Menurepository.FirstOrDefaultAsync(t => t.Id == menu.MenuId);
            await Menurepository.UpdateAsync(list);
            return APIResult.OK(data: list);
        }
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <returns></returns>
        [HttpDelete("DelMenu")]
        public async Task<APIResult> DelMenu(Guid id)
        {
            var list = await Menurepository.FirstOrDefaultAsync(t => t.Id == id);
            await Menurepository.DeleteAsync(list);
            return APIResult.OK(data: list);
        }
    }

}